
source('utils.R')
source('read_dataset.R')

donors = list(
  d12_inds = open_dataset(glue('final_long/cycle=2012')) %>% filter(d_pcc > 0 | d_sp > 0 | d_jfc > 0) %>% pull(component),
  r12_inds = open_dataset(glue('final_long/cycle=2012')) %>% filter(r_pcc > 0 | r_sp > 0 | r_jfc > 0) %>% pull(component),
  d16_inds = open_dataset(glue('final_long/cycle=2016')) %>% filter(d_pcc > 0 | d_sp > 0 | d_jfc > 0) %>% pull(component),
  r16_inds = open_dataset(glue('final_long/cycle=2016')) %>% filter(r_pcc > 0 | r_sp > 0 | r_jfc > 0) %>% pull(component),
  d20_inds = open_dataset(glue('final_long/cycle=2020')) %>% filter(d_pcc > 0 | d_sp > 0 | d_jfc > 0) %>% pull(component),
  r20_inds = open_dataset(glue('final_long/cycle=2020')) %>% filter(r_pcc > 0 | r_sp > 0 | r_jfc > 0) %>% pull(component)
)

t12 = merge(
  data.table(id = donors$d12_inds, d12 = 1),
  data.table(id = donors$r12_inds, r12 = 1),
  by = 'id',
  all = T
)

t16 = merge(
  data.table(id = donors$d16_inds, d16 = 1),
  data.table(id = donors$r16_inds, r16 = 1),
  by = 'id',
  all = T
)

t20 = merge(
  data.table(id = donors$d20_inds, d20 = 1),
  data.table(id = donors$r20_inds, r20 = 1),
  by = 'id',
  all = T
)

t12 = merge(t12, df12[, .(
  id = component,
  nat_quant12 = as.numeric(nat_quant) > 9,
  d = d_val,
  r = r_val
)], by = 'id', all.x = T)

t16 = merge(t16, df16[, .(
  id = component,
  nat_quant16 = as.numeric(nat_quant) > 9,
  d = d_val,
  r = r_val
)], by = 'id', all.x = T)

t20 = merge(t20, df20[, .(
  id = component,
  nat_quant20 = as.numeric(nat_quant) > 9,
  d = d_val,
  r = r_val
)], by = 'id', all.x = T)

t = merge(t12, t16, by = 'id', all = T)
t = merge(t, t20, by = 'id', all = T)

setnafill(t, fill = 0, cols = which(grepl('^(d|r)',names(t))))

t[,nat_quant_best := NA]
t[,nat_quant_best:=fifelse(!is.na(nat_quant20),nat_quant20,nat_quant_best)]
t[,nat_quant_best:=fifelse(!is.na(nat_quant16),nat_quant16,nat_quant_best)]
t[,nat_quant_best:=fifelse(!is.na(nat_quant12),nat_quant12,nat_quant_best)]

setnames(t, 'nat_quant_best', 'top_10')

t = t[,.N,by = list(d12,r12,d16,r16,d20,r20,top_10)]

# Rs and Ds

t1 = t[, .(N = sum(N)), .(d12, r12, d16, r16, d20, r20)]

t1[, dtot := d12 + d16 + d20]
t1[, rtot := r12 + r16 + r20]

t1 = t1[!(dtot > 0 & rtot > 0)]

out_rd = rbindlist(list(
  t1[rtot > 0][, party := 'Republican Donors'],
  t1[dtot > 0][, party := 'Democratic Donors']
))

out_rd[, year2012 := ifelse(r12 | d12, 'Donor', 'Non-Donor')]
out_rd[, year2016 := ifelse(r16 | d16, 'Donor', 'Non-Donor')]
out_rd[, year2020 := ifelse(r20 | d20, 'Donor', 'Non-Donor')]

out_rd = out_rd[, c(7, 10:13)]

cols = names(out_rd)[3:5]

out_rd[, (cols) := lapply(.SD, \(x) factor(x, levels = c('Non-Donor', 'Donor'))), .SDcols = cols]

# Rs only, by wealth

t2 = t[!is.na(top_10), .(N = sum(N)), .(d12, r12, d16, r16, d20, r20, top_10)]

t2[, dtot := d12 + d16 + d20]
t2[, rtot := r12 + r16 + r20]

t2 = t2[!(dtot > 0 & rtot > 0)]

out_rs = rbindlist(list(
  t2[rtot > 0][, party := 'Republican Donors'],
  t2[dtot > 0][, party := 'Democratic Donors']
))


out_rs[, year2012 := ifelse(r12 | d12, 'Donor', 'Non-Donor')]
out_rs[, year2016 := ifelse(r16 | d16, 'Donor', 'Non-Donor')]
out_rs[, year2020 := ifelse(r20 | d20, 'Donor', 'Non-Donor')]

out_rs = out_rs[, c(7:8, 11:14)]

cols = names(out_rs)[4:6]
out_rs[, (cols) := lapply(.SD, \(x) factor(x, levels = c('Non-Donor', 'Donor'))), .SDcols = cols]

out_rs[, pct_gr := N / sum(N), .(top_10, party)]

out_rs = out_rs[party == 'Republican Donors']
out_rs[, top_10 := fifelse(top_10,
                           'Matched Donors in the Top 10%',
                           'Matched Donors in the Bottom 90%')]

out_rs[, fill := fcase(
  year2012 == 'Donor', 1,
  year2012 == 'Non-Donor' & year2016 == 'Non-Donor' & year2020=='Donor', 2,
  default = 3
)]

save(out_rs, file = 'summary_data/table1.rda')
save(out_rd, file = 'summary_data/tableA11.rda')

